package com.xpec.c4.db.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ms.service.module.analysis.GameNameWithHallType;
import com.ms.service.module.analysis.MemberType;
import com.ms.service.module.analysis.PlatformType;
import com.ms.service.module.analysis.PlayerEnterSeatStatusType;
import com.ms.service.module.analysis.PlayerSeatData;
import com.xpec.c4.db.basic.BasicGameDAO;
import com.xpec.c4.db.hibernate.manager.HibernateLogManager;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.db.model.PlayerKeepSeatClearLogModel;
import com.xpec.c4.db.model.PlayerKeepSeatModel;
import com.xpec.c4.db.model.basic.BasicGameModel;
import com.xpec.c4.game.utility.GameUtility;

public class PlayerKeepSeatDAO extends BasicGameDAO {
	
	
	/**
	 * 讀取個人保留資料
	 * @param guid
	 * @return
	 */
	public static PlayerKeepSeatModel  getKeepSeatByGuid(String guid)
	{
		Session session  = HibernateManager.getSession();
		Query query = session.createQuery("from PlayerKeepSeatModel Where guid=:GUID");
		query.setString("GUID", guid);
		PlayerKeepSeatModel model = (PlayerKeepSeatModel) query.uniqueResult();
		HibernateManager.endSession();
		return model;
	}
	
	
	/**
	 * 新增 或 更新 保留資料
	 * @param model
	 * @return
	 */
	public static boolean modifyKeepSeatData(PlayerKeepSeatModel model)
	{
		Session session  = HibernateManager.txBegin();
		session.saveOrUpdate(model);
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	
	/**
	 * 讀取所有資料
	 * @param model
	 * @return
	 */
	public static List<PlayerKeepSeatModel> getKeepData(int min , int max)
	{
		Session session  = HibernateManager.getSession();
		Query query = session.createQuery("from PlayerKeepSeatModel");
		List<PlayerKeepSeatModel> list = (List<PlayerKeepSeatModel>)query.list();
		HibernateManager.endSession();
		return list;
	}
	
	/**
	 * 將待清除的座位保留清單寫入 db
	 * 
	 * @param seatClearLogList
	 */
	public static void saveWaitClearKeepSeatData(ArrayList<PlayerKeepSeatClearLogModel> seatClearLogList) {
		if (GameUtility.getArrayListSize(seatClearLogList) == 0) {
			return;
		}

		Session s = HibernateLogManager.txBegin();

		for (PlayerKeepSeatClearLogModel cl : seatClearLogList) {
			s.saveOrUpdate(cl);
		}

		HibernateLogManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 從 db 載入待清除的座位保留清單
	 * 
	 * @return
	 */
	public static ArrayList<PlayerKeepSeatClearLogModel> loadWaitClearKeepSeatData() {
		Session s = HibernateLogManager.getSession();

		Query query = s.createQuery("from PlayerKeepSeatClearLogModel where status=:status");
		query.setParameter("status", PlayerKeepSeatClearLogModel.WAIT_CLEAR);

		@SuppressWarnings("unchecked")
		ArrayList<PlayerKeepSeatClearLogModel> list = (ArrayList<PlayerKeepSeatClearLogModel>) query.list();

		HibernateManager.endSession();

		return list;
	}
	
	
	/**
	 * 取得離線SPIN的玩家資料(原本是用在analysis在開機時初始化使用，因為需求移除暫時沒有用到)
	 * @return
	 */
	public static HashMap<String, PlayerSeatData> getOfflineSpinPlayerSeatData(){
		HashMap<String, PlayerSeatData> offlineSpinPlayerSeatData = new HashMap<String, PlayerSeatData>();
		Session session =  HibernateManager.getSession();

		ArrayList<String> contentList = new ArrayList<String>();
		Query query = session.createQuery(
				"select content from PlayerKeepSeatModel where content like '%\"machinestat\":4%'");
		contentList = (ArrayList<String>)query.list();
		
		for(String content : contentList){
			PlayerSeatData playerSeatData = new PlayerSeatData();
			//取得content的各種參數
			JsonParser parser = new JsonParser();
			JsonObject rootObejct = parser.parse(content).getAsJsonObject();
			JsonArray contentArray = rootObejct.getAsJsonArray("keep");
			for (JsonElement gameKeepSeatData: contentArray) {
				//如果機台狀態是離線SPIN就要加入
				if(Integer.valueOf(gameKeepSeatData.getAsJsonObject().get("machinestat").toString()) == BasicGameModel.OFFLINE_KEEP_SPIN){
					//如果遊戲館名稱沒有在ENUM中就跳過
					String gameName = gameKeepSeatData.getAsJsonObject().get("game").toString().replace("\"", "");
					String hallType = gameKeepSeatData.getAsJsonObject().get("hall").toString().replace("\"", "");
					if(GameNameWithHallType.getENUMbyString(gameName, hallType) == null)
						continue;
					else{
						playerSeatData.setGameNameWithHallType(GameNameWithHallType.getENUMbyString(gameName, hallType));
						//從content取得GUID
						query = session.createQuery("select guid from PlayerKeepSeatModel where content =:C");
						query.setParameter("C", content);
						String guid = (String)query.uniqueResult();
						//從GUID在playermodel取得lastLoginPlatform
						query = session.createQuery("select lastLoginPlatform from PlayerModel where guid =:G");
						query.setParameter("G", guid);
						String lastLoginPlatform = (String)query.uniqueResult();
						//如果玩家的平台有誤就跳過
						if(PlatformType.getENUMbyString(lastLoginPlatform) == null)
							continue;
						else{
							playerSeatData.setPlatformType(PlatformType.getENUMbyString(lastLoginPlatform));
							playerSeatData.setMemberType(MemberType.OFFLINE_SPIN_MEMBER);
							playerSeatData.setPlayerEnterSeatStatusType(PlayerEnterSeatStatusType.OFFLINE_SPIN);
							offlineSpinPlayerSeatData.put(guid, playerSeatData);
						}
					}
				}
			}
		}
		HibernateManager.endSession();
		return offlineSpinPlayerSeatData;
	}
		
}
